import { screen, Size, sys, view } from "cc";
import { Platform } from "./common/enums";
import { AppCfg } from "./AppCfgs";
import { IBaseSdk } from "./sdk/IBaseSdk";
import { WxSdk } from "./sdk/impl/WxSdk";
import { LogMgr } from "../platform/log/LogMgr";
import { GlobalEventMgr } from "../platform/event/GlobalEventMgr";
import { GlobalEventTyp } from "./common/GlobalEventType";

declare type PlatformCfgType = typeof AppCfg["wx"]
export class GameCtx {
    private static instance: GameCtx = null;
    public static get ins() {
        if (this.instance == null) {
            this.instance = new GameCtx()
        }
        return this.instance
    }

    //屏幕物理大小
    _screenSize: Size = null;
    public get screenSize(){
        return this._screenSize
    }
    //设计大小
    designSize: Size = null
    //舞台大小（ui实际操作的大小）
    stageSize: Size = null

    safeX: number = 0
    safeY: number = 0

    //平台
    platform: Platform = Platform.web
    //平台提供的实例
    platformIns: any = null

    //平台下的游戏配置
    platformCfg: PlatformCfgType = null

    sdk: IBaseSdk = null;

    dataFrom:string = "local"


    init() {
        this.deviceInit()
        this.platformInit()
        this.sdkInit()

    }
    deviceInit() {
        let screenWidth: number = screen.resolution.width / view.getScaleX();
        let screenHeight: number = screen.resolution.height / view.getScaleY();
        this._screenSize = new Size(screenWidth, screenHeight);
        console.log('cc screen', this._screenSize);
    }
    sdkInit() {
        switch (this.platform) {
            case Platform.wechat:
                this.sdk = new WxSdk(this.platformIns);
                this.sdk.createVideo();
                break;
            case Platform.web:
                break;
        }
    }
    platformInit() {
        //平台初始化
        this.platformCfg = AppCfg["web"]
        let platform = sys.platform
        if (platform == sys.Platform.WECHAT_GAME) {//微信小游戏
            //https://developers.weixin.qq.com/minigame/dev/api/open-api/user-info/wx.getUserInfo.html
            this.platform = Platform.wechat
            this.platformIns = window["wx"];
            let env = this.platformIns.getAccountInfoSync().miniProgram.envVersion;
            switch (env) {
                case "release":
                    this.platformCfg = AppCfg["wx"];
                    break;
                case "develop":
                case "trial":
                    // this._cfg = PlatformConfig["wx"];
                    this.platformCfg = AppCfg["wx_test"];
                    break;
            }
            // this._cfg = PlatformConfig["wx"];
            this.platformIns.setKeepScreenOn({
                keepScreenOn: true
            })
            this.platformIns.onShow((msg) => {
                LogMgr.info("wx onShow", msg);
                //微信后台回来恢复音乐播放
                GlobalEventMgr.ins.emit(GlobalEventTyp.ReuseMusic);
                //微信后台回来后，获取微信游戏圈数据
                // GlobalEventMgr.ins.emit(GlobalEventTyp.ON_SHOW_GAME_CLUB);

                // if (this.initOver) {
                //     EventManager.ins.emit(EventType.CHECK_REWARD);
                // }
            });
            this.platformIns.onHide((msg) => {
                GlobalEventMgr.ins.emit(GlobalEventTyp.PauseMusic);
                // GlobalEventMgr.ins.emit(EventType.CLOSE_SHARE_VIEW);
                LogMgr.info("wx onHide", msg);
            });
        }

        this.dataFrom = this.platformCfg.data
    }
}